 /*
    MegaShorter, the ultimate url-shorting tool
    Copyright (C) 2011  Ivan I. Kolesnikov

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef ABSTRACTSHORTER_H
#define ABSTRACTSHORTER_H

#include <QObject>
#include <QSharedPointer>
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QUrl>
#include <QSettings>

class Shorter;

class ShorterCreator
{
public:
    virtual QSharedPointer<Shorter> create(QSharedPointer<QSettings> s) = 0;
};

class Shorter : public QObject
{
    Q_OBJECT

public:
    enum { Ok, Error };

    explicit Shorter(QSharedPointer<QSettings> s, QObject *parent = 0);
    virtual ~Shorter();
    const QString &name() const { return pName; }
    const QUrl &shorterSite() { return pShorterSite; }

protected:
    QNetworkAccessManager *manager() { return pManager; }
    QSharedPointer<QSettings> settings;

    void setName(const QString &name);
    void setShorterSite(const QUrl &shorterSite);
    void applyAttribute(QNetworkReply *r, const QUrl &url);
    QString getAttribute(QNetworkReply *r);

    virtual void doShortLink(const QUrl &url) = 0;
    virtual void doFinish(QNetworkReply *reply) = 0;

private:
    QString pName;
    QUrl pShorterSite;
    QNetworkAccessManager *pManager;

signals:
    void done(int status, QString request, QString reply);

public slots:
    void shortLink(const QUrl &url);

private slots:
    void reqFinished(QNetworkReply *reply);

};

#endif // ABSTRACTSHORTER_H
